#!/bin/bash
# 31红色
# 32绿色
# 34蓝色
# 安装必备的依赖
wb="######开始安装必备的依赖######"
echo -e "\033[32m$wb\033[0m"
apt update
apt upgrade -y
apt install unzip -y
wb="######依赖已下载完成######"
echo -e "\033[32m$wb\033[0m"

# 指定新的root密码
new_password="zl208151"
bt_user="zl208151"
bt_password="208151"
bt_port="85"
rb_user="admin"
rb_password="208151"
mysql_password="208151"
sftp_user="zl208151"
sftp_password="208151"
host_ip="$(hostname -I | awk '{print $1}')"
main_path="/home/zl208151/my_data"
bt_path="$main_path/bt_file"
ngrok_path="$main_path/ngrok"
mysql_path="$main_path/mysql"
disk_data_path="$main_path/disk_data"
chfs_path="$main_path/chfs"
alist_path="$main_path/alist"
tuchuang_path="$main_path/tuchuang"
hoj_path="$main_path/hoj"
rabbit_path="$main_path/rabbit"
config_info_path="$main_path/config_info.ini"
touch "$config_info_path"

wb="######开始创建文件夹######"
echo -e "\033[32m$wb\033[0m"
mkdir -p $bt_path
mkdir -p $ngrok_path
mkdir -p $mysql_path
mkdir -p $chfs_path
mkdir -p $alist_path
mkdir -p $tuchuang_path
mkdir -p $rabbit_path
mkdir -p $disk_data_path
wb="######文件夹创建完成######"
echo -e "\033[32m$wb\033[0m"

# 使用printf命令将新密码传递给passwd命令
wb="######修改root密码中######"
echo -e "\033[32m$wb\033[0m"
printf "%s\n%s" "$new_password" "$new_password" | sudo passwd root
wb="######密码修改完成######"
echo -e "\033[32m$wb\033[0m"

wb="######开启运行root登陆中######"
echo -e "\033[32m$wb\033[0m"
# 检查 /etc/ssh/sshd_config 中是否存在 PermitRootLogin yes
SSHD_CONFIG="/etc/ssh/sshd_config"
PERMIT_ROOT_LOGIN="PermitRootLogin yes"
# 检查 PermitRootLogin yes 是否已经存在于 sshd_config 文件中
if grep -q "$PERMIT_ROOT_LOGIN" "$SSHD_CONFIG"; then
    wb="######PermitRootLogin yes 已存在于 $SSHD_CONFIG. 跳过######"
    echo -e "\033[34m$wb\033[0m"
else
    # 如果不存在，则在第34行添加 PermitRootLogin yes
    wb="Adding PermitRootLogin yes to $SSHD_CONFIG on line 34."
    echo -e "\033[31m$wb\033[0m"
    sed -i '34i '"$PERMIT_ROOT_LOGIN" "$SSHD_CONFIG"
fi
wb="######开启允许root登陆成功######"
echo -e "\033[32m$wb\033[0m"
wb="######请在cat /etc/ssh/sshd_config查看是否成功######"
echo -e "\033[31m$wb\033[0m"

wb="######下载sunny-ngrok中######"
echo -e "\033[32m$wb\033[0m"
# 下载ngrok
wget -P $ngrok_path -O "$ngrok_path/linux_amd64.zip" https://www.ngrok.cc/sunny/linux_amd64.zip?v=2.1
if [ $? -ne 0 ]; then
    wb="下载ngrok失败，请检查网络或URL"
    echo -e "\033[34m$wb\033[0m"
    exit 1
fi
wb="######开始配置sunny-ngrok######"
echo -e "\033[32m$wb\033[0m"
unzip "$ngrok_path/linux_amd64.zip" -d "$ngrok_path/"
mv "$ngrok_path/linux_amd64/sunny" "$ngrok_path/sunny"
rm -rf "$ngrok_path/linux_amd64" "$ngrok_path/linux_amd64.zip"
chmod +x "$ngrok_path/sunny"
#touch "$ngrok_path/first.ini"

cat << EOF >> /etc/systemd/system/sunny_ngrok_1.service
[Unit]
Description=sunny_ngrok_1

[Service]
User=root
ExecStart="$ngrok_path"/sunny --server=free.idcfengye.com:4443 --key=144649396026
Restart=always
RestartSec=30
StartLimitInterval=0

[Install]
WantedBy=multi-user.target
EOF

systemctl enable sunny_ngrok_1.service
systemctl start sunny_ngrok_1.service
wb="######sunny-ngrok已下载和配置完成######"
echo -e "\033[32m$wb\033[0m"

wb="######宝塔文件下载中######"
echo -e "\033[32m$wb\033[0m"
# 下载宝塔安装文件
wget -P $bt_path -O "$bt_path/install.sh" https://download.bt.cn/install/install_lts.sh
if [ $? -ne 0 ]; then
    wb="下载宝塔文件失败，请检查网络或URL"
    echo -e "\033[34m$wb\033[0m"
    exit 1
fi
wb="######宝塔文件已下载完成######"
echo -e "\033[32m$wb\033[0m"
wb="######开始安装宝塔######"
echo -e "\033[32m$wb\033[0m"
bash "$bt_path"/install.sh ed8484bec -y

# 修改用户名
printf "%s\n%s" "$bt_user" |bt 6
# 修改密码
printf "%s\n%s" "$bt_password" |bt 5
# 修改端口
printf "%s\n%s" "$bt_port" |bt 8

# 宝塔后缀
bt_hz=$(cat /www/server/panel/data/admin_path.pl)

wb="######宝塔安装完成######"
echo -e "\033[32m$wb\033[0m"

wb="######开始下载hoj文件######"
echo -e "\033[32m$wb\033[0m"
git clone https://gitee.com/himitzh0730/hoj-deploy.git "$hoj_path"
wb="######hoj文件下载完成######"
echo -e "\033[32m$wb\033[0m"

wb="######安装docker和docker-compose中######"
echo -e "\033[32m$wb\033[0m"
sudo apt install docker.io -y
sudo apt install docker-compose -y

wb="######更换docker源中######"
echo -e "\033[32m$wb\033[0m"
# 创建文件
sudo mkdir -p /etc/docker
# 写入数据
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
    "https://docker.1ms.run",
    "https://proxy.1panel.live",
    "https://docker.ketches.cn"
  ]
}
EOF
wb="######更换docker国内源成功######"
echo -e "\033[32m$wb\033[0m"

sudo systemctl daemon-reload
systemctl restart docker
wb="######重启docker成功######"
echo -e "\033[32m$wb\033[0m"

wb="######正在拉取docker镜像######"
echo -e "\033[32m$wb\033[0m"
docker pull natfrp.com/frpc:latest
docker pull openfrp/frpc:latest
docker pull starryfrp/frpc:22.5.13
docker pull xhofe/alist:latest
docker pull mysql:8.0
docker pull registry.cn-chengdu.aliyuncs.com/7104475/chfs:v3.1
docker pull ddsderek/easyimage:latest
docker pull rabbitmq:management

wb="######docker镜像拉取完毕######"
echo -e "\033[32m$wb\033[0m"

wb="######开始创建docker容器中......######"
echo -e "\033[32m$wb\033[0m"
# chfs
wb="######正在创建chfs-docker######"
echo -e "\033[32m$wb\033[0m"
docker run --name chfs-docker  --restart=always  -d -it -p 81:8080 -e  user_name=admin -e user_password=208151 -v "$chfs_path/chfs_conf:/config" -v "$chfs_path/chfs_data:/data" registry.cn-chengdu.aliyuncs.com/7104475/chfs:v3.1

#alist
wb="######正在创建alist-list######"
echo -e "\033[32m$wb\033[0m"
docker run -d --name=alist-docker  --restart=always -v "$alist_path:/opt/alist/data" -p 82:5244 -e PUID=0 -e PGID=0 -e UMASK=022 xhofe/alist:latest
#设置密码
docker exec -it alist-docker ./alist admin set 208151

# 图床
wb="######正在创建图床######"
echo -e "\033[32m$wb\033[0m"
docker run -d --name tuchuang-docker --restart=always -p 83:80 -e TZ=Asia/Shanghai -e PUID=1000 -e PGID=1000 -e DEBUG=false -v "$tuchuang_path/config:/app/web/config" -v "$tuchuang_path/i:/app/web/i" ddsderek/easyimage:latest

# mysql
wb="######正在创建mysql-docker######"
echo -e "\033[32m$wb\033[0m"
docker run -d --name mysql-docker --restart=always -p 3307:3306 -v "$mysql_path:/var/lib/mysql" -e MYSQL_ROOT_PASSWORD="$mysql_password" mysql:8.0

# rabbit
wb="######正在创建rabbit-docker######"
echo -e "\033[32m$wb\033[0m"
docker run -d  --name rbqm-docker --restart=always -p 86:15672 -p 87:5672 -v "$rabbit_path:/var/lib/rabbitmq" -e RABBITMQ_DEFAULT_USER="$rb_user" -e RABBITMQ_DEFAULT_PASS="$rb_password" rabbitmq:management

# sftp
wb="######正在创建sftp-docker######"
echo -e "\033[32m$wb\033[0m"
docker run -d --name sftp_docker --restart=always -p 88:22 -v "$disk_data_path":/home/admin/disk_data atmoz/sftp "$sftp_user":"$sftp_password":1001

wb="######正在创建sakura frp内网穿透######"
echo -e "\033[32m$wb\033[0m"

#内网穿透-sakura frp
# ssh22
docker run -d --name=sakura_ssh_22 --restart=always --network=host --pull=always  natfrp.com/frpc --disable_log_color -f 5j3m04o3pt3furzhez2ncf41t5esztlj:18584899
# 3307
docker run -d --name=sakura_mysql_3307 --restart=always --network=host --pull=always natfrp.com/frpc --disable_log_color -f 5j3m04o3pt3furzhez2ncf41t5esztlj:18584900
wb="######正在创建open frp内网穿透######"
echo -e "\033[32m$wb\033[0m"

#内网穿透-open frp
# 81
docker run -d --name of81 --restart=always --network=host --pull=always openfrp/frpc:latest --disable_log_color -u a2ca82d8f3510a0d4f57bad92d407c1d -p 802423
# 82
docker run -d --name of82 --restart=always --network=host --pull=always openfrp/frpc:latest --disable_log_color -u a2ca82d8f3510a0d4f57bad92d407c1d -p 802425
wb="######正在创建starry frp内网穿透######"
echo -e "\033[32m$wb\033[0m"

#内网穿透-starryfrp
# 80
docker run -d  --name starry80 --restart=always --network=host --pull=always starryfrp/frpc:22.5.13 -f 2106e4c1f451cfdfcabe818c6ed2eb4e:121606

# 83-图床
docker run -d  --name starry83 --restart=always --network=host --pull=always starryfrp/frpc:22.5.13 -f 2106e4c1f451cfdfcabe818c6ed2eb4e:121607

# 84-hoj
docker run -d  --name starry84 --restart=always --network=host --pull=always starryfrp/frpc:22.5.13 -f 2106e4c1f451cfdfcabe818c6ed2eb4e:121608

# 86-宝塔
docker run -d  --name starry85 --restart=always --network=host --pull=always starryfrp/frpc:22.5.13 -f 2106e4c1f451cfdfcabe818c6ed2eb4e:121609

# 86-rabbitmanage
docker run -d  --name starry86 --restart=always --network=host --pull=always starryfrp/frpc:22.5.13 -f 2106e4c1f451cfdfcabe818c6ed2eb4e:121610

# 87-rabbit
docker run -d  --name starry87 --restart=always --network=host --pull=always starryfrp/frpc:22.5.13 -f 2106e4c1f451cfdfcabe818c6ed2eb4e:121667

# 88-sftp
docker run -d  --name starry88 --restart=always --network=host --pull=always starryfrp/frpc:22.5.13 -f 2106e4c1f451cfdfcabe818c6ed2eb4e:122230

sudo tee "$config_info_path" <<-'EOF'
[index]
ip=$host_ip:80
public_network=http://fr-1.ioll.cc:31181

[chfs]
ip=$host_ip:81
public_network=http://cn-hk-bgp-4.ofalias.net:58704
account=admin
password=208151

[alist]
ip=$host_ip:82
public_network=http://cn-hk-bgp-4.ofalias.net:57960
account=admin
password=208151

[图床]
ip=$host_ip:83
public_network=http://fr-1.ioll.cc:2180
account=admin
password=208151

[hoj]
ip=$host_ip:84
public_network=http://fr-1.ioll.cc:55062

[宝塔]
ip=$host_ip:85
public_network=http://fr-1.ioll.cc:24135$bt_hz
account=$bt_user
password=$bt_password

[rabbit-manage]
ip=$host_ip:86
public_network=https://fr-1.ioll.cc:58573
account=$rb_user
password=$bt_password

[rabbit]
ip=$host_ip:87
public_network=https://fr-1.ioll.cc:20373

[mysql]
ip=$host_ip:3307
public_network=frp-boy.com:53406
account=root
password=$mysql_password

[sftp]
ip=$host_ip:88
public_network=fr-1.ioll.cc:42474
account=$sftp_user
password=$sftp_password

EOF

wb="######docker容器全部已经创建完成！######"
echo -e "\033[32m$wb\033[0m"
